package com.ziyun.pop.mall.module.order.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.service.cart.model.cart.AddCartDto;
import com.service.common.constant.AppType;
import com.service.common.constant.CartType;
import com.service.common.constant.OperateType;
import com.service.order.api.OrderService;
import com.service.order.api.PayService;
import com.service.order.constant.OrderResult;
import com.service.order.constant.TaxType;
import com.service.order.model.cart.OrderAddCartDto;
import com.service.order.model.order.SaveOrderParamDto;
import com.service.order.model.order.SaveOrderRequestVO;
import com.service.order.model.order.SettlementParamDto;
import com.service.order.model.order.SettlementResultVo;
import com.service.order.model.order.UserShip;
import com.service.order.model.pay.OrderPayInfoResultDto;
import com.service.quote.module.quote.model.QuoteDto;
import com.service.ucenter.api.UserTaxService;
import com.service.ucenter.model.tax.UserTaxVO;
import com.service.utils.push.umeng.JsonResult;
import com.utility.JsonUtils;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 单个商品去下单，购物车商品区去结算
 * 
 * @author chenxiaoyi
 * 
 */

@Controller
public class GoDownController {
	private static Logger _log = LoggerFactory.getLogger(GoDownController.class);

	private static final String MODEL = "/order/popGoDown";

	@Autowired
	private OrderService orderService;

	@Autowired
	private UserTaxService userTaxService;

	@Autowired
	private HttpRequestLocal httpRequestLocal;

	@Autowired
	private PayService payService;

	/**
	 * 单个标准商品点击去下单页面
	 * 
	 * @param factoryId
	 * @param str
	 *            页面参数
	 * @return
	 */
	@PostMapping(MODEL + "/bzRedirectIndex.json")
	@ResponseBody
	public JsonResult<?> bzRedirectIndex(String addCartDto) {
		AddCartDto cartDto = JSON.parseObject(addCartDto, new TypeReference<AddCartDto>() {
		});
		OrderAddCartDto orderAddCartDto = new OrderAddCartDto();
		BeanUtils.copyProperties(cartDto, orderAddCartDto);
		orderAddCartDto.setOperateType(OperateType.buynow);
		httpRequestLocal.setSessionValue("orderAddCartDto", JsonUtils.object2String(orderAddCartDto));
		return JsonResult.success();

	}

	/**
	 * 单个报价商品点击去下单页面
	 * 
	 * @param factoryId
	 * @param str
	 *            页面参数
	 * @return
	 */
	@PostMapping(MODEL + "/bjRedirectIndex.json")
	@ResponseBody
	public JsonResult<?> bjRedirectIndex(String addCartDto) {
		AddCartDto cartDto = JsonUtils.string2Object(addCartDto, AddCartDto.class);
		String quoteDtoStr = (String) httpRequestLocal.getSessionValue("quoteDto");
		QuoteDto quoteDto = JsonUtils.string2Object(quoteDtoStr, QuoteDto.class);
		cartDto.setQuoteSelfDefineParamMap(quoteDto.getDefinitionValueMap());
		cartDto.setQuoteAttrOptionMap(quoteDto.getAttributesMap());
		cartDto.setSelfPaperParts(quoteDto.getSelfPaperParts());
		OrderAddCartDto orderAddCartDto = new OrderAddCartDto();
		BeanUtils.copyProperties(cartDto, orderAddCartDto);
		orderAddCartDto.setOperateType(OperateType.buynow);
		httpRequestLocal.setSessionValue("orderAddCartDto", JsonUtils.object2String(orderAddCartDto));
		return JsonResult.success();

	}

	@GetMapping(MODEL + "/index.do")
	@ResponseBody
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/order/goDown/index.html");
		return view;
	}

	/**
	 * 购物车点击去下单页面
	 * 
	 * @param factoryId
	 *            工厂id
	 * @return
	 */
	@GetMapping(MODEL + "/gwcIndex.do")
	@ResponseBody
	public ModelAndView gwcIndex(HttpServletRequest request, HttpServletResponse response) {
		String cartType = request.getParameter("cartType");
		ModelAndView view = new ModelAndView("/order/goDown/gwcIndex.html");
		view.addObject("cartType", CartType.valueOf(cartType));
		return view;
	}

	// 初始化获取结算页信息
	@PostMapping(MODEL + "/getSettlementIndex.json")
	@ResponseBody
	public JsonResult<SettlementResultVo> getSettlementIndex() {
		String orderAddCartDtoStr = (String) httpRequestLocal.getSessionValue("orderAddCartDto");
		OrderAddCartDto orderAddCartDto = JsonUtils.string2Object(orderAddCartDtoStr, OrderAddCartDto.class);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		SettlementResultVo result = orderService.getSpecialSettlement(AppType.pc ,userId,orderAddCartDto).getData();
		return JsonResult.success(result);
	}

	// 获取结算页信息
	@PostMapping(MODEL + "/getSettlement.json")
	@ResponseBody
	public JsonResult<SettlementResultVo> getSettlement(String requestData) {
		SettlementParamDto paramDto = JsonUtils.string2Object(requestData, SettlementParamDto.class);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		SettlementResultVo result = orderService.getSettlement(AppType.pc ,userId,paramDto).getData();
		return JsonResult.success(result);
	}

	// 下单结算
	@PostMapping(MODEL + "/settlementGoDown.json")
	@ResponseBody
	public JsonResult<OrderResult<SaveOrderRequestVO>> settlementGoDown(String requestData) {
		SaveOrderParamDto orderParamDto = JsonUtils.string2Object(requestData, SaveOrderParamDto.class);
		if (orderParamDto.getTaxType() == TaxType.companySpecial) {
			Long userId = (Long) httpRequestLocal.getSessionValue("userId");
			UserTaxVO userTaxVO = userTaxService.getTax(userId, orderParamDto.getTaxId()).getData();
			BeanUtils.copyProperties(userTaxVO, orderParamDto);
		}
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		OrderResult<SaveOrderRequestVO> result = orderService.saveOrder(AppType.pc ,userId,orderParamDto);
		return JsonResult.success(result);
	}

	/**
	 * 购物车点击去下单成功页面
	 * 
	 * @param
	 * @return
	 */
	@GetMapping(MODEL + "/goDownSucess.do")
	public ModelAndView goDownSucess(HttpServletRequest request, HttpServletResponse response) {
		String orderId = request.getParameter("orderId");
		ModelAndView view = new ModelAndView("/order/goDown/goDownSucess.html");
		view.addObject("orderId", orderId);
		return view;
	}

	/**
	 * 订单支付信息页面
	 * 
	 * @param
	 * @return
	 */
	@PostMapping(MODEL + "/getPayOrderInfo.json")
	@ResponseBody
	public JsonResult<OrderPayInfoResultDto> getPayOrderInfo(HttpServletRequest request, HttpServletResponse response) {
		String orderId = request.getParameter("orderId");
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		OrderPayInfoResultDto payOrderResultDto = payService.getOrderPayInfo(AppType.pc ,userId,orderId).getData();
		return JsonResult.success(payOrderResultDto);
	}

	/**
	 * 获取用户自提信息
	 * 
	 * @param
	 * @return
	 */
	@PostMapping(MODEL + "/getUserShip.json")
	@ResponseBody
	public JsonResult<UserShip> getUserShip(HttpServletRequest request, HttpServletResponse response) {
		UserShip userShip = orderService.getUserShip().getData();
		// UserShip userShip = UserShip.builder().build();
		return JsonResult.success(userShip);

	}

}
